home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / winsr173.zip / HELP.C < prev    next >
C/C++ Source or Header  |  1992-03-15  |  38KB  |  1,677 lines

  1.  
  2. /*
  3.  * help.c
  4.  *
  5.  * This module is linked as an overlay, use ENTER_OVLY and EXIT_OVLY.
  6.  *
  7.  *
  8.  * Revision history:
  9.  *
  10.  *   2-26-90  EAN     Initial version.
  11.  *
  12.  *
  13.  */
  14.  
  15.  
  16. #ifndef TEST /* kills all those assert macros in production version */
  17. #define NDEBUG
  18. #endif
  19.  
  20. #define INCLUDE_COMMON    /* include common code in helpcom.h */
  21.  
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <io.h>
  26. #include <fcntl.h>
  27. #include <dos.h>
  28. #include <string.h>
  29. #include <time.h>
  30. #include <assert.h>
  31. #include <sys\types.h>
  32. #include <sys\stat.h>
  33. #include "fractint.h"
  34. #include "helpcom.h"
  35. #include "helpdefs.h"
  36.  
  37.  
  38. #define MAX_HIST       16         /* number of pages we'll remember */
  39.  
  40. #define ALT_F1         1104
  41. #define BACK_TAB     1015
  42. #define BACKSPACE        8
  43.  
  44. #define ACTION_CALL        0         /* values returned by help_topic() */
  45. #define ACTION_PREV        1
  46. #define ACTION_PREV2        2         /* special - go back two topics */
  47. #define ACTION_INDEX        3
  48. #define ACTION_QUIT        4
  49.  
  50. #define F_HIST            (1<<0)   /* flags for help_topic() */
  51. #define F_INDEX         (1<<1)
  52.  
  53. #define MAX_PAGE_SIZE        (80*25)  /* no page of text may be larger */
  54.  
  55. #define TEXT_START_ROW        2         /* start print the help text here */
  56.  
  57.  
  58. typedef struct
  59.    {
  60.    unsigned char r, c;
  61.    int         width;
  62.    unsigned     offset;
  63.    int         topic_num;
  64.    unsigned     topic_off;
  65.    } LINK;
  66.  
  67.  
  68. typedef struct
  69.    {
  70.    int        topic_num;
  71.    unsigned topic_off;
  72.    } LABEL;
  73.  
  74.  
  75. typedef struct
  76.    {
  77.    unsigned     offset;
  78.    unsigned     len;
  79.    int         margin;
  80.    } PAGE;
  81.  
  82.  
  83. typedef struct
  84.    {
  85.    int        topic_num;
  86.    unsigned topic_off;
  87.    int        link;
  88.    } HIST;
  89.  
  90.  
  91. struct help_sig_info
  92.    {
  93.    unsigned long sig;
  94.    int         version;
  95.    unsigned long base;       /* only if added to fractint.exe */
  96.    } ;
  97.  
  98.  
  99. void print_document(char *outfname, int (*msg_func)(int,int), int save_extraseg );
  100. static int print_doc_msg_func(int pnum, int num_pages);
  101.  
  102.  
  103.  
  104. void help_overlay(void) { }
  105.  
  106.  
  107.  
  108. /* stuff from fractint */
  109.  
  110. extern int  lookatmouse;
  111. extern long timer_start;
  112. extern int  helpmode;
  113. extern int  text_type;     /* 0=real color text, 1=640x200x2, 2=??mono?? */
  114. extern int  textcbase;
  115. extern int  textrbase;
  116. extern int  extraseg;
  117. extern int  release;
  118.  
  119. void putstring         (int row, int col, int attr, unsigned char far *msg);
  120. int  putstringcenter (int row, int col, int width, int attr, char far *msg);
  121. void setattr         (int row, int col, int attr, int width);
  122. void movecursor      (int row, int col);
  123. void setclear         (void);
  124. void helptitle         (void);
  125. int  getakey         (void);
  126. int  keypressed      (void);
  127. void stackscreen     (void);
  128. void unstackscreen   (void);
  129. void findpath         (char *filename, char *path);
  130. int  farread         (int handle, void far *buf, unsigned len);
  131. int  farwrite         (int handle, void far *buf, unsigned len);
  132.  
  133.  
  134. static int          help_file = -1; /* help file handle */
  135. static long          base_off;       /* offset to help info in help file */
  136. static int          max_links;      /* max # of links in any page */
  137. static int          max_pages;      /* max # of pages in any topic */
  138. static int          num_label;      /* number of labels */
  139. static int          num_topic;      /* number of topics */
  140. static int          curr_hist = 0;  /* current pos in history */
  141.  
  142. /* these items alloc'ed in init_help... */
  143.  
  144. static long     far *topic_offset;       /* 4*num_topic */
  145. static LABEL     far *label;           /* 4*num_label */
  146. static HIST     far *hist;           /* 6*MAX_HIST (96 bytes) */
  147.  
  148. /* these items alloc'ed only while help is active... */
  149.  
  150. static char      far *buffer;         /* MAX_PAGE_SIZE (2048 bytes) */
  151. static LINK      far *link_table;     /* 10*max_links */
  152. static PAGE      far *page_table;     /* 4*max_pages  */
  153.  
  154.  
  155. static void help_seek(long pos)
  156.    {
  157.    lseek(help_file, base_off+pos, SEEK_SET);
  158.    }
  159.  
  160.  
  161. static void displayc(int row, int col, int color, int ch)
  162.    {
  163.    static char *s = "?";
  164.  
  165.    if (text_type == 1)     /* if 640x200x2 mode */
  166.       {
  167.       /*
  168.        * This is REALLY ugly, but it works.  Non-current links (ones that
  169.        * would be bold if 640x200 supported it) are in upper-case and the
  170.        * current item is inversed.
  171.        *
  172.        */
  173.  
  174.       if (color & INVERSE)
  175.      color = INVERSE;
  176.       else if (color & BRIGHT)
  177.      {
  178.      color = 0;   /* normal */
  179.      if (ch>='a' && ch<='z')
  180.         ch += 'A' - 'a';
  181.      }
  182.       else
  183.      color = 0;   /* normal */
  184.       }
  185.  
  186.    s[0] = ch;
  187.    putstring(row, col, color, s);
  188.    }
  189.  
  190.  
  191. static void display_text(int row, int col, int color, char far *text, unsigned len)
  192.    {
  193.    if (text_type != 1) {  /* go-fast, if we can */
  194.        int i, j;
  195.        char texttemp;
  196.        for (i = 0; i < len; i++)
  197.            if (text[i] == CMD_LITERAL)
  198.                break;
  199.        if (i == len) {
  200.            texttemp = text[len];
  201.            text[len] = 0;
  202.            putstring(row, col, color, text);
  203.            text[len] = texttemp;
  204.            return;
  205.            }
  206.        }
  207.        
  208.    while (len-- > 0)
  209.       {
  210.       if (*text == CMD_LITERAL)
  211.      {
  212.      ++text;
  213.      --len;
  214.      }
  215.       displayc(row, col++, color, *text++);
  216.       }
  217.    }
  218.  
  219.  
  220. static void display_parse_text(char far *text, unsigned len, int start_margin, int *num_link, LINK far *link)
  221.    {
  222.    char far  *curr;
  223.    int          row, col;
  224.    int          tok;
  225.    int          size,
  226.           width;
  227.  
  228.    textcbase = SCREEN_INDENT;
  229.    textrbase = TEXT_START_ROW;
  230.  
  231.    curr = text;
  232.    row = 0;
  233.    col = 0;
  234.  
  235.    size = width = 0;
  236.  
  237.    if (start_margin >= 0)
  238.       tok = TOK_PARA;
  239.    else
  240.       tok = -1;
  241.  
  242.    while ( 1 )
  243.       {
  244.       switch ( tok )
  245.      {
  246.      case TOK_PARA:
  247.         {
  248.         int indent,
  249.         margin;
  250.  
  251.         if (size > 0)
  252.            {
  253.            ++curr;
  254.            indent = *curr++;
  255.            margin = *curr++;
  256.            len  -= 3;
  257.            }
  258.         else
  259.            {
  260.            indent = start_margin;
  261.            margin = start_margin;
  262.            }
  263.  
  264.         col = indent;
  265.  
  266.         while (1)
  267.            {
  268.            tok = find_token_length(ONLINE, curr, len, &size, &width);
  269.  
  270.            if (tok == TOK_DONE || tok == TOK_NL || tok == TOK_FF )
  271.           break;
  272.  
  273.            if (tok == TOK_PARA)
  274.           {
  275.           col = 0;   /* fake a new-line */
  276.           row++;
  277.           break;
  278.           }
  279.  
  280.            if (tok == TOK_XONLINE || tok == TOK_XDOC)
  281.           {
  282.           curr += size;
  283.           len  -= size;
  284.           continue;
  285.           }
  286.  
  287.            /* now tok is TOK_SPACE or TOK_LINK or TOK_WORD */
  288.  
  289.            if (col+width > SCREEN_WIDTH)
  290.           {         /* go to next line... */
  291.           col = margin;
  292.           ++row;
  293.  
  294.           if ( tok == TOK_SPACE )
  295.              width = 0;   /* skip spaces at start of a line */
  296.           }
  297.  
  298.            if (tok == TOK_LINK)
  299.           {
  300.           display_text(row, col, C_HELP_LINK, curr+7, width);
  301.           if (num_link != NULL)
  302.              {
  303.              link[*num_link].r           = row;
  304.              link[*num_link].c           = col;
  305.              link[*num_link].topic_num = *( (int far *) (curr+1) );
  306.              link[*num_link].topic_off = *( (int far *) (curr+3) );
  307.              link[*num_link].offset    = (unsigned) ((curr+7) - text);
  308.              link[*num_link].width     = width;
  309.              ++(*num_link);
  310.              }
  311.           }
  312.            else if (tok == TOK_WORD )
  313.           display_text(row, col, C_HELP_BODY, curr, width);
  314.  
  315.            col += width;
  316.            curr += size;
  317.            len -= size;
  318.            }
  319.  
  320.         width = size = 0;
  321.         break;
  322.         }
  323.  
  324.      case TOK_CENTER:
  325.         col = find_line_width(ONLINE, curr, len);
  326.         col = (SCREEN_WIDTH-col)/2;
  327.         if (col < 0)
  328.            col = 0;
  329.         break;
  330.  
  331.      case TOK_NL:
  332.         col = 0;
  333.         ++row;
  334.         break;
  335.  
  336.      case TOK_LINK:
  337.         display_text(row, col, C_HELP_LINK, curr+7, width);
  338.         if (num_link != NULL)
  339.            {
  340.            link[*num_link].r     = row;
  341.            link[*num_link].c     = col;
  342.            link[*num_link].topic_num = *( (int far *) (curr+1) );
  343.            link[*num_link].topic_off = *( (int far *) (curr+3) );
  344.            link[*num_link].offset     = (unsigned) ((curr+7) - text);
  345.            link[*num_link].width     = width;
  346.            ++(*num_link);
  347.            }
  348.         break;
  349.  
  350.      case TOK_XONLINE:  /* skip */
  351.      case TOK_FF:        /* ignore */
  352.      case TOK_XDOC:     /* ignore */
  353.      case TOK_DONE:
  354.      case TOK_SPACE:
  355.         break;
  356.  
  357.      case TOK_WORD:
  358.         display_text(row, col, C_HELP_BODY, curr, width);
  359.         break;
  360.      } /* switch */
  361.  
  362.       curr += size;
  363.       len  -= size;
  364.       col  += width;
  365.  
  366.       if (len == 0)
  367.      break;
  368.  
  369.       tok = find_token_length(ONLINE, curr, len, &size, &width);
  370.       } /* while */
  371.  
  372.    textcbase = 0;
  373.    textrbase = 0;
  374.    }
  375.  
  376.  
  377. static void color_link(LINK far *link, int color)
  378.    {
  379.    textcbase = SCREEN_INDENT;
  380.    textrbase = TEXT_START_ROW;
  381.  
  382.    if (text_type == 1)     /* if 640x200x2 mode */
  383.       display_text(link->r, link->c, color, buffer+link->offset, link->width);
  384.    else
  385.       setattr(link->r, link->c, color, link->width);
  386.  
  387.    textcbase = 0;
  388.    textrbase = 0;
  389.    }
  390.  
  391.  
  392. static void blank_row24()
  393. {
  394. char row24[81];
  395.  
  396. memset(row24,32,80);
  397. row24[80] = 0;
  398. putstring(24,0,C_HELP_INSTR,row24);
  399.  
  400. }
  401.  
  402.  
  403. /* #define PUT_KEY(name, descrip) putstring(-1,-1,C_HELP_INSTR_KEYS,name), putstring(-1,-1,C_HELP_INSTR," "descrip"  ") */
  404. #define PUT_KEY(name, descrip) putstring(-1,-1,C_HELP_INSTR,name); putstring(-1,-1,C_HELP_INSTR,":"descrip"  ")
  405.  
  406.  
  407. static void helpinstr(void)
  408.    {
  409.    int ctr;
  410.  
  411.    blank_row24();
  412.  
  413.    movecursor(24, 1);
  414.    PUT_KEY("F1",               "Index");
  415.    PUT_KEY("\x18\x19\x1B\x1A", "Select");
  416.    PUT_KEY("Enter",            "Go to");
  417.    PUT_KEY("Backspace",        "Last topic");
  418.    PUT_KEY("Escape",           "Exit help");
  419.    }
  420.  
  421.  
  422. static void printinstr(void)
  423.    {
  424.    int ctr;
  425.  
  426.    blank_row24();
  427.  
  428.    movecursor(24, 1);
  429.    PUT_KEY("Escape", "Abort");
  430.    }
  431.  
  432.  
  433. #undef PUT_KEY
  434.  
  435.  
  436. static void display_page(char far *title, char far *text, unsigned text_len, int page, int num_pages, int start_margin, int *num_link, LINK far *link)
  437.    {
  438.    char temp[9];
  439.  
  440.    helptitle();
  441.    helpinstr();
  442.    setattr(2, 0, C_HELP_BODY, 80*22);
  443.    putstringcenter(1, 0, 80, C_HELP_HDG, title);
  444.    sprintf(temp, "%2d of %d", page+1, num_pages);
  445.    putstring(1, 79-(6 + ((num_pages>=10)?2:1)), C_HELP_INSTR, temp);
  446.  
  447.    if (text != NULL)
  448.       display_parse_text(text, text_len, start_margin, num_link, link);
  449.  
  450.    movecursor(25, 80);     /* hide cursor */
  451.    }
  452.  
  453.  
  454.  
  455. /*
  456.  * int overlap(int a, int a2, int b, int b2);
  457.  *
  458.  * If a, a2, b, and b2 are points on a line, this function returns the
  459.  * distance of intersection between a-->a2 and b-->b2.    If there is no
  460.  * intersection between the lines this function will return a negative number
  461.  * representing the distance between the two lines.
  462.  *
  463.  * There are six possible cases of intersection between the lines:
  464.  *
  465.  *            a              a2
  466.  *            |              |
  467.  *     b     b2    |              |       b     b2
  468.  *     |---(1)---|    |              |       |---(2)---|
  469.  *            |              |
  470.  *        b    |     b2      b       |      b2
  471.  *        |------(3)----|       |------(4)-----|
  472.  *            |              |
  473.  *         b    |              |   b2
  474.  *         |------+--------(5)----------+---|
  475.  *            |              |
  476.  *            |     b       b2      |
  477.  *            |     |--(6)--|       |
  478.  *            |              |
  479.  *            |              |
  480.  *
  481.  */
  482.  
  483.  
  484. static int overlap(int a, int a2, int b, int b2)
  485.    {
  486.    if ( b < a )
  487.       {
  488.       if ( b2 >= a2 )
  489.      return ( a2 - a );           /* case (5) */
  490.  
  491.       return ( b2 - a );           /* case (1), case (3) */
  492.       }
  493.  
  494.    if ( b2 <= a2 )
  495.       return ( b2 - b );           /* case (6) */
  496.  
  497.    return ( a2 - b );               /* case (2), case (4) */
  498.    }
  499.  
  500.  
  501. static int dist(int a, int b)
  502.    {
  503.    int t = a - b;
  504.  
  505.    return (abs(t));
  506.    }
  507.  
  508.  
  509. #ifdef __TURBOC__
  510. #   pragma warn -def /* turn off "Possible use before definition" warning */
  511. #endif
  512.  
  513.  
  514.  
  515.  
  516. static int find_link_updown(LINK far *link, int num_link, int curr_link, int up)
  517.    {
  518.    int         ctr,
  519.          curr_c2,
  520.          best_overlap,
  521.          temp_overlap;
  522.    LINK far *curr,
  523.     far *temp,
  524.     far *best;
  525.    int         temp_dist;
  526.  
  527.    curr    = &link[curr_link];
  528.    best    = NULL;
  529.    curr_c2 = curr->c + curr->width - 1;
  530.  
  531.    for (ctr=0, temp=link; ctr<num_link; ctr++, temp++)
  532.       {
  533.       if ( ctr != curr_link &&
  534.        ( (up && temp->r < curr->r) || (!up && temp->r > curr->r) ) )
  535.      {
  536.      temp_overlap = overlap(curr->c, curr_c2, temp->c, temp->c+temp->width-1);
  537.      /* if >= 3 lines between, prioritize on vertical distance: */
  538.      if ((temp_dist = dist(temp->r, curr->r)) >= 4)
  539.         temp_overlap -= temp_dist * 100;
  540.  
  541.      if (best != NULL)
  542.         {
  543.         if ( best_overlap >= 0 && temp_overlap >= 0 )
  544.            {     /* if they're both under curr set to closest in y dir */
  545.            if ( dist(best->r, curr->r) > temp_dist )
  546.           best = NULL;
  547.            }
  548.         else
  549.            {
  550.            if ( best_overlap < temp_overlap )
  551.           best = NULL;
  552.            }
  553.         }
  554.  
  555.      if (best == NULL)
  556.         {
  557.         best = temp;
  558.         best_overlap = temp_overlap;
  559.         }
  560.      }
  561.       }
  562.  
  563.    return ( (best==NULL) ? -1 : (int)(best-link) );
  564.    }
  565.  
  566.  
  567. static int find_link_leftright(LINK far *link, int num_link, int curr_link, int left)
  568.    {
  569.    int         ctr,
  570.          curr_c2,
  571.          best_c2,
  572.          temp_c2,
  573.          best_dist,
  574.          temp_dist;
  575.    LINK far *curr,
  576.     far *temp,
  577.     far *best;
  578.  
  579.    curr    = &link[curr_link];
  580.    best    = NULL;
  581.    curr_c2 = curr->c + curr->width - 1;
  582.  
  583.    for (ctr=0, temp=link; ctr<num_link; ctr++, temp++)
  584.       {
  585.       temp_c2 = temp->c + temp->width - 1;
  586.  
  587.       if ( ctr != curr_link &&
  588.        ( (left && temp_c2 < curr->c) || (!left && temp->c > curr_c2) ) )
  589.      {
  590.      temp_dist = dist(curr->r, temp->r);
  591.  
  592.      if (best != NULL)
  593.         {
  594.         if ( best_dist == 0 && temp_dist == 0 )  /* if both on curr's line... */
  595.            {
  596.            if ( (  left && dist(curr->c, best_c2) > dist(curr->c, temp_c2) ) ||
  597.             ( !left && dist(curr_c2, best->c) > dist(curr_c2, temp->c) ) )
  598.           best = NULL;
  599.            }
  600.         else
  601.            {
  602.            if ( best_dist >= temp_dist )   /* if temp is closer... */
  603.           best = NULL;
  604.            }
  605.         } /* if (best...) */
  606.  
  607.      if (best == NULL)
  608.         {
  609.         best      = temp;
  610.         best_dist = temp_dist;
  611.         best_c2   = temp_c2;
  612.         }
  613.      }
  614.       } /* for */
  615.  
  616.    return ( (best==NULL) ? -1 : (int)(best-link) );
  617.    }
  618.  
  619.  
  620. #ifdef __TURBOC__
  621. #   pragma warn .def   /* back to default */
  622. #   pragma warn -par   /* now turn off "Parameter not used" warning */
  623. #endif
  624.  
  625.  
  626. static int find_link_key(LINK far *link, int num_link, int curr_link, int key)
  627.    {
  628.    switch (key)
  629.       {
  630.       case TAB:      return ( (curr_link>=num_link-1) ? -1 : curr_link+1 );
  631.       case BACK_TAB: return ( (curr_link<=0)          ? -1 : curr_link-1 );
  632.       default:         assert(0);  return (-1);
  633.       }
  634.    }
  635.  
  636.  
  637. #ifdef __TURBOC__
  638. #   pragma warn .par /* back to default */
  639. #endif
  640.  
  641.  
  642. static int do_move_link(LINK far *link, int num_link, int *curr, int (*f)(LINK far *,int,int,int), int val)
  643.    {
  644.    int t;
  645.  
  646.    if (num_link > 1)
  647.       {
  648.       if ( f == NULL )
  649.      t = val;
  650.       else
  651.      t = (*f)(link, num_link, *curr, val);
  652.  
  653.       if ( t >= 0 && t != *curr )
  654.      {
  655.      color_link(&link[*curr], C_HELP_LINK);
  656.      *curr = t;
  657.      color_link(&link[*curr], C_HELP_CURLINK);
  658.      return (1);
  659.      }
  660.       }
  661.  
  662.    return (0);
  663.    }
  664.  
  665.  
  666. static int help_topic(HIST *curr, HIST *next, int flags)
  667.    {
  668.    int         len;
  669.    int         key;
  670.    int         num_pages;
  671.    int         num_link;
  672.    int         page;
  673.    int         curr_link;
  674.    char      title[81];
  675.    long      where;
  676.    int         draw_page;
  677.    int         action;
  678.  
  679.    where     = topic_offset[curr->topic_num]+2; /* +2 to skip flags */
  680.    curr_link = curr->link;
  681.  
  682.    help_seek(where);
  683.  
  684.    read(help_file, (char *)&num_pages, sizeof(int));
  685.    assert(num_pages>0 && num_pages<=max_pages);
  686.  
  687.    farread(help_file, (char far *)page_table, 6*num_pages);
  688.  
  689.    len = 0;
  690.    read(help_file, (char *)&len, 1);
  691.    assert(len>=0 && len<81);
  692.    read(help_file, (char *)title, len);
  693.    title[len] = '\0';
  694.  
  695.    where += 2 + num_pages*6 + 1 + len + 2;
  696.  
  697.    for(page=0; page<num_pages; page++)
  698.       if (curr->topic_off >= page_table[page].offset &&
  699.       curr->topic_off <  page_table[page].offset+page_table[page].len )
  700.      break;
  701.  
  702.    assert(page < num_pages);
  703.  
  704.    action = -1;
  705.    draw_page = 2;
  706.  
  707.    do
  708.       {
  709.       if (draw_page)
  710.      {
  711.      help_seek(where+page_table[page].offset);
  712.      farread(help_file, buffer, page_table[page].len);
  713.  
  714.      num_link = 0;
  715.      display_page(title, buffer, page_table[page].len, page, num_pages,
  716.               page_table[page].margin, &num_link, link_table);
  717.  
  718.      if (draw_page==2)
  719.         {
  720.         assert(num_link<=0 || (curr_link>=0 && curr_link<num_link));
  721.         }
  722.      else if (draw_page==3)
  723.         curr_link = num_link - 1;
  724.      else
  725.         curr_link = 0;
  726.  
  727.      if (num_link > 0)
  728.         color_link(&link_table[curr_link], C_HELP_CURLINK);
  729.  
  730.      draw_page = 0;
  731.      }
  732.  
  733.       key = getakey();
  734.  
  735.       switch(key)
  736.      {
  737.      case PAGE_DOWN:
  738.         if (page<num_pages-1)
  739.            {
  740.            page++;
  741.            draw_page = 1;
  742.            }
  743.         break;
  744.  
  745.      case PAGE_UP:
  746.         if (page>0)
  747.            {
  748.            page--;
  749.            draw_page = 1;
  750.            }
  751.         break;
  752.  
  753.      case HOME:
  754.         if ( page != 0 )
  755.            {
  756.            page = 0;
  757.            draw_page = 1;
  758.            }
  759.         else
  760.            do_move_link(link_table, num_link, &curr_link, NULL, 0);
  761.         break;
  762.  
  763.      case END:
  764.         if ( page != num_pages-1 )
  765.            {
  766.            page = num_pages-1;
  767.            draw_page = 3;
  768.            }
  769.         else
  770.            do_move_link(link_table, num_link, &curr_link, NULL, num_link-1);
  771.         break;
  772.  
  773.      case TAB:
  774.         if ( !do_move_link(link_table, num_link, &curr_link, find_link_key, key) &&
  775.          page<num_pages-1 )
  776.            {
  777.            ++page;
  778.            draw_page = 1;
  779.            }
  780.         break;
  781.  
  782.      case BACK_TAB:
  783.         if ( !do_move_link(link_table, num_link, &curr_link, find_link_key, key) &&
  784.          page>0 )
  785.            {
  786.            --page;
  787.            draw_page = 3;
  788.            }
  789.         break;
  790.  
  791.      case DOWN_ARROW:
  792.         if ( !do_move_link(link_table, num_link, &curr_link, find_link_updown, 0) &&
  793.          page<num_pages-1 )
  794.            {
  795.            ++page;
  796.            draw_page = 1;
  797.            }
  798.         break;
  799.  
  800.      case UP_ARROW:
  801.         if ( !do_move_link(link_table, num_link, &curr_link, find_link_updown, 1) &&
  802.          page>0 )
  803.            {
  804.            --page;
  805.            draw_page = 3;
  806.            }
  807.         break;
  808.  
  809.      case LEFT_ARROW:
  810.         do_move_link(link_table, num_link, &curr_link, find_link_leftright, 1);
  811.         break;
  812.  
  813.      case RIGHT_ARROW:
  814.         do_move_link(link_table, num_link, &curr_link, find_link_leftright, 0);
  815.         break;
  816.  
  817.      case ESC:       /* exit help */
  818.         action = ACTION_QUIT;
  819.         break;
  820.  
  821.      case BACKSPACE:   /* prev topic */
  822.      case ALT_F1:
  823.         if (flags & F_HIST)
  824.            action = ACTION_PREV;
  825.         break;
  826.  
  827.      case F1:    /* help index */
  828.         if (!(flags & F_INDEX))
  829.            action = ACTION_INDEX;
  830.         break;
  831.  
  832.      case ENTER:
  833.      case ENTER_2:
  834.         if (num_link > 0)
  835.            {
  836.            next->topic_num = link_table[curr_link].topic_num;
  837.            next->topic_off = link_table[curr_link].topic_off;
  838.            action = ACTION_CALL;
  839.            }
  840.         break;
  841.      } /* switch */
  842.       }
  843.    while ( action == -1 );
  844.  
  845.    curr->topic_off = page_table[page].offset;
  846.    curr->link       = curr_link;
  847.  
  848.    return (action);
  849.    }
  850.  
  851.  
  852. int help(int action)
  853.    {
  854.    static char far unknowntopic_msg[] = "Unknown Help Topic";
  855.    HIST      curr;
  856.    int         oldlookatmouse;
  857.    int         oldhelpmode;
  858.    int         flags;
  859.    HIST      next;
  860.  
  861.    ENTER_OVLY(OVLY_HELP);
  862.  
  863.    if (helpmode == -1)     /* is help disabled? */
  864.       {
  865.       EXIT_OVLY;
  866.       return (0);
  867.       }
  868.  
  869.    if (help_file == -1)
  870.       {
  871. #ifdef WINFRACT
  872.       static char far fractint_not_there[] = "Can't find FRACTINT.EXE, so \nFractint-style help is not available";
  873.       stopmsg(0,fractint_not_there);
  874. #else
  875.       buzzer(2);
  876. #endif
  877.       EXIT_OVLY;
  878.       return (0);
  879.       }
  880.  
  881.    buffer = farmemalloc((long)MAX_PAGE_SIZE + sizeof(LINK)*max_links +
  882.             sizeof(PAGE)*max_pages);
  883.  
  884.    if (buffer == NULL)
  885.       {
  886.       buzzer(2);
  887.       EXIT_OVLY;
  888.       return (0);
  889.       }
  890.  
  891.    link_table = (LINK far *)(&buffer[MAX_PAGE_SIZE]);
  892.    page_table = (PAGE far *)(&link_table[max_links]);
  893.  
  894.    oldlookatmouse = lookatmouse;
  895.    lookatmouse = 0;
  896.    timer_start -= clock();
  897.    stackscreen();
  898.  
  899.    if (helpmode >= 0)
  900.       {
  901.       next.topic_num = label[helpmode].topic_num;
  902.       next.topic_off = label[helpmode].topic_off;
  903.       }
  904.    else
  905.       {
  906.       next.topic_num = helpmode;
  907.       next.topic_off = 0;
  908.       }
  909.  
  910.    oldhelpmode = helpmode;
  911.  
  912.    if (curr_hist <= 0)
  913.       action = ACTION_CALL;  /* make sure it isn't ACTION_PREV! */
  914.  
  915.    do
  916.       {
  917.       switch(action)
  918.      {
  919.      case ACTION_PREV2:
  920.         if (curr_hist > 0)
  921.            curr = hist[--curr_hist];
  922.  
  923.         /* fall-through */
  924.  
  925.      case ACTION_PREV:
  926.         if (curr_hist > 0)
  927.            curr = hist[--curr_hist];
  928.         break;
  929.  
  930.      case ACTION_QUIT:
  931.         break;
  932.  
  933.      case ACTION_INDEX:
  934.         next.topic_num = label[HELP_INDEX].topic_num;
  935.         next.topic_off = label[HELP_INDEX].topic_off;
  936.  
  937.         /* fall-through */
  938.  
  939.      case ACTION_CALL:
  940.         curr = next;
  941.         curr.link = 0;
  942.         break;
  943.      } /* switch */
  944.  
  945.       flags = 0;
  946.       if (curr.topic_num == label[HELP_INDEX].topic_num)
  947.      flags |= F_INDEX;
  948.       if (curr_hist > 0)
  949.      flags |= F_HIST;
  950.  
  951.       if ( curr.topic_num >= 0 )
  952.      action = help_topic(&curr, &next, flags);
  953.       else
  954.      {
  955.      if ( curr.topic_num == -100 )
  956.         {
  957.         print_document("FRACTINT.DOC", print_doc_msg_func, 1);
  958.         action = ACTION_PREV2;
  959.         }
  960.  
  961.      else if ( curr.topic_num == -101 )
  962.         action = ACTION_PREV2;
  963.  
  964.      else
  965.         {
  966.         display_page(unknowntopic_msg, NULL, 0, 0, 1, 0, NULL, NULL);
  967.         action = -1;
  968.         while (action == -1)
  969.            {
  970.            switch (getakey())
  971.           {
  972.           case ESC:     action = ACTION_QUIT;    break;
  973.           case ALT_F1:     action = ACTION_PREV;    break;
  974.           case F1:     action = ACTION_INDEX; break;
  975.           } /* switch */
  976.            } /* while */
  977.         }
  978.      } /* else */
  979.  
  980.       if ( action != ACTION_PREV && action != ACTION_PREV2 )
  981.      {
  982.      if (curr_hist >= MAX_HIST)
  983.         {
  984.         int ctr;
  985.  
  986.         for (ctr=0; ctr<MAX_HIST-1; ctr++)
  987.            hist[ctr] = hist[ctr+1];
  988.  
  989.         curr_hist = MAX_HIST-1;
  990.         }
  991.      hist[curr_hist++] = curr;
  992.      }
  993.       }
  994.    while (action != ACTION_QUIT);
  995.  
  996.    farmemfree((unsigned char far *)buffer);
  997.  
  998.    unstackscreen();
  999.    lookatmouse = oldlookatmouse;
  1000.    helpmode = oldhelpmode;
  1001.    timer_start += clock();
  1002.  
  1003.    EXIT_OVLY;
  1004.    return(0);
  1005.    }
  1006.  
  1007.  
  1008.  
  1009. static int dos_version(void)
  1010.    {
  1011.    union REGS r;
  1012.  
  1013.    r.x.ax = 0x3000;
  1014.    intdos(&r, &r);
  1015.  
  1016.    return (r.h.al*100 + r.h.ah);
  1017.    }
  1018.  
  1019.  
  1020. static int exe_path(char *filename, char *path)
  1021.    {
  1022.    char *ptr;
  1023.  
  1024.    if (dos_version() >= 300)  /* DOS version 3.00+ ? */
  1025.       {
  1026. #ifdef __TURBOC__
  1027.       strcpy(path, _argv[0]);
  1028. #else  /* assume MSC */
  1029.       extern char **__argv;
  1030.       strcpy(path, __argv[0]);     /* note: __argv may be undocumented in MSC */
  1031. #endif
  1032.  
  1033.       ptr = strrchr(path, '\\');
  1034.       if (ptr == NULL)
  1035.      ptr = path;
  1036.       else
  1037.      ++ptr;
  1038.       strcpy(ptr, filename);
  1039.       return (1);
  1040.       }
  1041.  
  1042.    return (0);
  1043.    }
  1044.  
  1045.  
  1046. static int find_file(char *filename, char *path)
  1047.    {
  1048.    int handle;
  1049.  
  1050.    if ( exe_path(filename, path) )
  1051.       if ( (handle=open(path, O_RDONLY)) != -1)
  1052.      {
  1053.      close(handle);
  1054.      return (1);
  1055.      }
  1056.  
  1057.    findpath(filename,path);
  1058.    return ( (path[0]) ? 1 : 0);
  1059.    }
  1060.  
  1061.  
  1062. static int _read_help_topic(int topic, int off, int len, void far *buf)
  1063.    {
  1064.    static int  curr_topic = -1;
  1065.    static long curr_base;
  1066.    static int  curr_len;
  1067.    int           read_len;
  1068.  
  1069.    if ( topic != curr_topic )
  1070.       {
  1071.       int t;
  1072.  
  1073.       curr_topic = topic;
  1074.  
  1075.       curr_base = topic_offset[topic];
  1076.  
  1077.       curr_base += 2;                   /* skip flags */
  1078.  
  1079.       help_seek(curr_base);
  1080.       read(help_file, (char *)&t, 2);           /* read num_pages */
  1081.       curr_base += 2 + t*6;               /* skip page info */
  1082.  
  1083.       if (t>0)
  1084.      help_seek(curr_base);
  1085.       t = 0;
  1086.       read(help_file, (char *)&t, 1);           /* read title_len */
  1087.       curr_base += 1 + t;               /* skip title */
  1088.  
  1089.       if (t>0)
  1090.      help_seek(curr_base);
  1091.       read(help_file, (char *)&curr_len, 2);   /* read topic len */
  1092.       curr_base += 2;
  1093.       }
  1094.  
  1095.    read_len = (off+len > curr_len) ? curr_len - off : len;
  1096.  
  1097.    if (read_len > 0)
  1098.       {
  1099.       help_seek(curr_base + off);
  1100.       farread(help_file, (char far *)buf, read_len);
  1101.       }
  1102.  
  1103.    return ( curr_len - (off+len) );
  1104.    }
  1105.  
  1106.  
  1107. int read_help_topic(int label_num, int off, int len, void far *buf)
  1108.    /*
  1109.     * reads text from a help topic.  Returns number of bytes from (off+len)
  1110.     * to end of topic.    On "EOF" returns a negative number representing
  1111.     * number of bytes not read.
  1112.     */
  1113.    {
  1114.    int ret;
  1115.  
  1116.    ENTER_OVLY(OVLY_HELP);
  1117.  
  1118.    if (help_file == -1)
  1119.       return (0);
  1120.    else
  1121.        ret = _read_help_topic(label[label_num].topic_num,
  1122.                   label[label_num].topic_off + off, len, buf);
  1123.  
  1124.    EXIT_OVLY;
  1125.  
  1126.    return ( ret );
  1127.    }
  1128.  
  1129.  
  1130. #define PRINT_BUFFER_SIZE  (32767)     /* max. size of help topic in doc. */
  1131. #define TEMP_FILE_NAME       "HELP.$$$"    /* temp file for storing extraseg  */
  1132.                      /*    while printing document        */
  1133. #define MAX_NUM_TOPIC_SEC  (10)      /* max. number of topics under any */
  1134.                      /*    single section (CONTENT)     */
  1135.  
  1136.  
  1137. typedef struct PRINT_DOC_INFO
  1138.    {
  1139.    int         cnum;        /* current CONTENT num */
  1140.    int         tnum;        /* current topic num */
  1141.  
  1142.    long      content_pos;   /* current CONTENT item offset in file */
  1143.    int         num_page;        /* total number of pages in document */
  1144.  
  1145.    int         num_contents,  /* total number of CONTENT entries */
  1146.          num_topic;     /* number of topics in current CONTENT */
  1147.  
  1148.    int         topic_num[MAX_NUM_TOPIC_SEC]; /* topic_num[] for current CONTENT entry */
  1149.  
  1150.    char far *buffer;        /* text buffer */
  1151.  
  1152.    char      id[81];        /* buffer to store id in */
  1153.    char      title[81];     /* buffer to store title in */
  1154.  
  1155.    int       (*msg_func)(int pnum, int num_page);
  1156.  
  1157.    FILE     *file;        /* file to sent output to */
  1158.    int         margin;        /* indent text by this much */
  1159.    int         start_of_line; /* are we at the beginning of a line? */
  1160.    int         spaces;        /* number of spaces in a row */
  1161.    } PRINT_DOC_INFO;
  1162.  
  1163.  
  1164. void print_document(char *outfname, int (*msg_func)(int,int), int save_extraseg );
  1165.  
  1166.  
  1167. static void printerc(PRINT_DOC_INFO *info, int c, int n)
  1168.    {
  1169.    while ( n-- > 0 )
  1170.       {
  1171.       if (c==' ')
  1172.      ++info->spaces;
  1173.  
  1174.       else if (c=='\n' || c=='\f')
  1175.      {
  1176.      info->start_of_line = 1;
  1177.      info->spaces = 0;   /* strip spaces before a new-line */
  1178.      putc(c, info->file);
  1179.      }
  1180.  
  1181.       else
  1182.      {
  1183.      if (info->start_of_line)
  1184.         {
  1185.         info->spaces += info->margin;
  1186.         info->start_of_line = 0;
  1187.         }
  1188.  
  1189.      while (info->spaces > 0)
  1190.         {
  1191.         fputc(' ', info->file);
  1192.         --info->spaces;
  1193.         }
  1194.  
  1195.      fputc(c, info->file);
  1196.      }
  1197.       }
  1198.    }
  1199.  
  1200.  
  1201. static void printers(PRINT_DOC_INFO *info, char far *s, int n)
  1202.    {
  1203.    if (n > 0)
  1204.       {
  1205.       while ( n-- > 0 )
  1206.      printerc(info, *s++, 1);
  1207.       }
  1208.    else
  1209.       {
  1210.       while ( *s != '\0' )
  1211.      printerc(info, *s++, 1);
  1212.       }
  1213.    }
  1214.  
  1215.  
  1216. static int print_doc_get_info(int cmd, PD_INFO *pd, PRINT_DOC_INFO *info)
  1217.    {
  1218.    int t;
  1219.  
  1220.    switch (cmd)
  1221.       {
  1222.       case PD_GET_CONTENT:
  1223.      if ( ++info->cnum >= info->num_contents )
  1224.         return (0);
  1225.  
  1226.      help_seek( info->content_pos );
  1227.  
  1228.      read(help_file, (char *)&t, 2);    /* read flags */
  1229.      info->content_pos += 2;
  1230.      pd->new_page = (t & 1) ? 1 : 0;
  1231.  
  1232.      t = 0;
  1233.      read(help_file, (char *)&t, 1);    /* read id len */
  1234.      assert(t<80);
  1235.      read(help_file, (char *)info->id, t);    /* read the id */
  1236.      info->content_pos += 1 + t;
  1237.      info->id[t] = '\0';
  1238.  
  1239.      t = 0;
  1240.      read(help_file, (char *)&t, 1);    /* read title len */
  1241.      assert(t<80);
  1242.      read(help_file, (char *)info->title, t); /* read the title */
  1243.      info->content_pos += 1 + t;
  1244.      info->title[t] = '\0';
  1245.  
  1246.      t = 0;
  1247.      read(help_file, (char *)&t, 1);    /* read num_topic */
  1248.      assert(t<MAX_NUM_TOPIC_SEC);
  1249.      read(help_file, (char *)info->topic_num, t*2);  /* read topic_num[] */
  1250.      info->num_topic = t;
  1251.      info->content_pos += 1 + t*2;
  1252.  
  1253.      info->tnum = -1;
  1254.  
  1255.      pd->id = info->id;
  1256.      pd->title = info->title;
  1257.      return (1);
  1258.  
  1259.       case PD_GET_TOPIC:
  1260.      if ( ++info->tnum >= info->num_topic )
  1261.         return (0);
  1262.  
  1263.      t = _read_help_topic(info->topic_num[info->tnum], 0, PRINT_BUFFER_SIZE, info->buffer);
  1264.  
  1265.      assert(t <= 0);
  1266.  
  1267.      pd->curr = info->buffer;
  1268.      pd->len  = PRINT_BUFFER_SIZE + t;   /* same as ...SIZE - abs(t) */
  1269.      return (1);
  1270.  
  1271.       case PD_GET_LINK_PAGE:
  1272.      pd->i = *(int far *)(pd->s+4);
  1273.      return ( (pd->i == -1) ? 0 : 1 );
  1274.  
  1275.       case PD_RELEASE_TOPIC:
  1276.      return (1);
  1277.  
  1278.       default:
  1279.      return (0);
  1280.       }
  1281.    }
  1282.  
  1283.  
  1284. static int print_doc_output(int cmd, PD_INFO *pd, PRINT_DOC_INFO *info)
  1285.    {
  1286.    switch (cmd)
  1287.       {
  1288.       case PD_HEADING:
  1289.      {
  1290.      char line[81];
  1291.      char buff[40];
  1292.      int  width = PAGE_WIDTH + PAGE_INDENT;
  1293.      int  keep_going;
  1294.  
  1295.      if ( info->msg_func != NULL )
  1296.         keep_going = (*info->msg_func)(pd->pnum, info->num_page);
  1297.      else
  1298.         keep_going = 1;
  1299.  
  1300.      info->margin = 0;
  1301.  
  1302.      memset(line, ' ', 81);
  1303.      sprintf(buff, "Fractint Version %d.%01d%c",release/100, (release%100)/10,
  1304.                 ( (release%10) ? '0'+(release%10) : ' ') );
  1305.      memmove(line + ( (width-strlen(buff)) / 2)-4, buff, strlen(buff));
  1306.  
  1307.      sprintf(buff, "Page %d", pd->pnum);
  1308.      memmove(line + (width - strlen(buff)), buff, strlen(buff));
  1309.  
  1310.      printerc(info, '\n', 1);
  1311.      printers(info, line, width);
  1312.      printerc(info, '\n', 2);
  1313.  
  1314.      info->margin = PAGE_INDENT;
  1315.  
  1316.      return ( keep_going );
  1317.      }
  1318.  
  1319.       case PD_FOOTING:
  1320.      info->margin = 0;
  1321.      printerc(info, '\f', 1);
  1322.      info->margin = PAGE_INDENT;
  1323.      return (1);
  1324.  
  1325.       case PD_PRINT:
  1326.      printers(info, pd->s, pd->i);
  1327.      return (1);
  1328.  
  1329.       case PD_PRINTN:
  1330.      printerc(info, *pd->s, pd->i);
  1331.      return (1);
  1332.  
  1333.       case PD_PRINT_SEC:
  1334.      info->margin = TITLE_INDENT;
  1335.      if (pd->id[0] != '\0')
  1336.         {
  1337.         printers(info, pd->id, 0);
  1338.         printerc(info, ' ', 1);
  1339.         }
  1340.      printers(info, pd->title, 0);
  1341.      printerc(info, '\n', 1);
  1342.      info->margin = PAGE_INDENT;
  1343.      return (1);
  1344.  
  1345.       case PD_START_SECTION:
  1346.       case PD_START_TOPIC:
  1347.       case PD_SET_SECTION_PAGE:
  1348.       case PD_SET_TOPIC_PAGE:
  1349.       case PD_PERIODIC:
  1350.      return (1);
  1351.  
  1352.       default:
  1353.      return (0);
  1354.       }
  1355.    }
  1356.  
  1357.  
  1358. static int print_doc_msg_func(int pnum, int num_pages)
  1359.    {
  1360.    char temp[10];
  1361.    int    key;
  1362.  
  1363.    if ( pnum == -1 )    /* successful completion */
  1364.       {
  1365.       buzzer(0);
  1366.       putstringcenter(7, 0, 80, C_HELP_LINK, "Done -- Press any key");
  1367.       getakey();
  1368.       return (0);
  1369.       }
  1370.  
  1371.    if ( pnum == -2 )   /* aborted */
  1372.       {
  1373.       buzzer(1);
  1374.       putstringcenter(7, 0, 80, C_HELP_LINK, "Aborted -- Press any key");
  1375.       getakey();
  1376.       return (0);
  1377.       }
  1378.  
  1379.    if (pnum == 0)   /* initialization */
  1380.       {
  1381.       helptitle();
  1382.       printinstr();
  1383.       setattr(2, 0, C_HELP_BODY, 80*22);
  1384.       putstringcenter(1, 0, 80, C_HELP_HDG, "Generating FRACTINT.DOC");
  1385.  
  1386.       putstring(7, 30, C_HELP_BODY, "Completed:");
  1387.  
  1388.       movecursor(25,80);   /* hide cursor */
  1389.       }
  1390.  
  1391.  
  1392.    sprintf(temp, "%d%%", (int)( (100.0 / num_pages) * pnum ) );
  1393.    putstring(7, 41, C_HELP_LINK, temp);
  1394.  
  1395.    while ( keypressed() )
  1396.       {
  1397.       key = getakey();
  1398.       if ( key == ESC )
  1399.      return (0);    /* user abort */
  1400.       }
  1401.  
  1402.    return (1);     /* AOK -- continue */
  1403.    }
  1404.  
  1405. int makedoc_msg_func(int pnum, int num_pages)
  1406.    {
  1407.    if (pnum >= 0)
  1408.       {
  1409.       printf("\rcompleted %d%%", (int)( (100.0 / num_pages) * pnum ) );
  1410.       return (1);
  1411.       }
  1412.    if ( pnum == -2 )
  1413.       printf("\n*** aborted");
  1414.    printf("\n");
  1415.    return (0);
  1416.    }
  1417.  
  1418.  
  1419.  
  1420. void print_document(char *outfname, int (*msg_func)(int,int), int save_extraseg )
  1421.    {
  1422.    static char far err_no_temp[]  = "Unable to create temporary file.\n";
  1423.    static char far err_no_out[]   = "Unable to create output file.\n";
  1424.    static char far err_badwrite[] = "Error writing temporary file.\n";
  1425.    static char far err_badread[]  = "Error reading temporary file.\nSystem may be corrupt!\nSave your image and re-start FRACTINT!\n";
  1426.  
  1427.    PRINT_DOC_INFO info;
  1428.    int          success;
  1429.    int          temp_file = -1;
  1430.    char      far *msg = NULL;
  1431.  
  1432.    ENTER_OVLY(OVLY_HELP);
  1433.  
  1434.    info.buffer = MK_FP(extraseg, 0);
  1435.  
  1436.    help_seek(8L);
  1437.    read(help_file, (char *)&info.num_contents, 2);
  1438.    read(help_file, (char *)&info.num_page, 2);
  1439.  
  1440.    info.cnum = info.tnum = -1;
  1441.    info.content_pos = 12 + num_topic*4 + num_label*4;
  1442.    info.msg_func = msg_func;
  1443.  
  1444.    if ( msg_func != NULL )
  1445.       msg_func(0, info.num_page);   /* initialize */
  1446.  
  1447.    if ( save_extraseg )
  1448.       {
  1449.       if ( (temp_file=open(TEMP_FILE_NAME, O_RDWR|O_CREAT|O_TRUNC|O_BINARY, S_IREAD|S_IWRITE)) == -1 )
  1450.      {
  1451.      msg = err_no_temp;
  1452.      goto ErrorAbort;
  1453.      }
  1454.  
  1455.       if ( farwrite(temp_file, info.buffer, PRINT_BUFFER_SIZE) != PRINT_BUFFER_SIZE )
  1456.      {
  1457.      msg = err_badwrite;
  1458.      goto ErrorAbort;
  1459.      }
  1460.       }
  1461.  
  1462.    if ( (info.file = fopen(outfname, "wt")) == NULL )
  1463.       {
  1464.       msg = err_no_out;
  1465.       goto ErrorAbort;
  1466.       }
  1467.  
  1468.    info.margin = PAGE_INDENT;
  1469.    info.start_of_line = 1;
  1470.    info.spaces = 0;
  1471.  
  1472.    success = process_document((PD_FUNC)print_doc_get_info,
  1473.                   (PD_FUNC)print_doc_output,   &info);
  1474.    fclose(info.file);
  1475.  
  1476.    if ( save_extraseg )
  1477.       {
  1478.       if ( lseek(temp_file, 0L, SEEK_SET) != 0L )
  1479.      {
  1480.      msg = err_badread;
  1481.      goto ErrorAbort;
  1482.      }
  1483.  
  1484.       if ( farread(temp_file, info.buffer, PRINT_BUFFER_SIZE) != PRINT_BUFFER_SIZE )
  1485.      {
  1486.      msg = err_badread;
  1487.      goto ErrorAbort;
  1488.      }
  1489.       }
  1490.  
  1491. ErrorAbort:
  1492.    if (temp_file != -1)
  1493.       {
  1494.       close(temp_file);
  1495.       remove(TEMP_FILE_NAME);
  1496.       temp_file = -1;
  1497.       }
  1498.  
  1499.    if ( msg != NULL )
  1500.       {
  1501.       helptitle();
  1502.       stopmsg(1, msg);
  1503.       }
  1504.  
  1505.    else if ( msg_func != NULL )
  1506.       msg_func((success) ? -1 : -2, info.num_page );
  1507.  
  1508.    EXIT_OVLY;
  1509.    }
  1510.  
  1511.  
  1512. int init_help(void)
  1513.    {
  1514.    struct help_sig_info hs;
  1515.    char         path[81];
  1516.  
  1517.    ENTER_OVLY(OVLY_HELP);
  1518.  
  1519.    help_file = -1;
  1520.  
  1521. #ifdef WINFRACT    /* leave this code out of the DOS version */
  1522.    /*
  1523.     * We don't worry about using far arrays for errors here since this code
  1524.     * won't be in the release version.
  1525.     */
  1526.  
  1527.    if ( find_file("FRACTINT.HLP", path) )
  1528.       {
  1529.       if ( (help_file = open(path, O_RDONLY|O_BINARY)) != -1 )
  1530.      {
  1531.      read(help_file, (char *)&hs, 6);
  1532.  
  1533.      if ( hs.sig != HELP_SIG )
  1534.         {
  1535.         static char far msg[] = {"Invalid help signature in FRACTINT.HLP!\n"};
  1536.     close(help_file);
  1537.     stopmsg(1, msg);
  1538.         }
  1539.  
  1540.      else if ( hs.version != HELP_VERSION )
  1541.         {
  1542.         static char far msg[] = {"Wrong help version in FRACTINT.HLP!\n"};
  1543.         close(help_file);
  1544.         stopmsg(1, msg);
  1545.         }
  1546.  
  1547.      else
  1548.         base_off = 6;
  1549.      }
  1550. /*  not an error - try FRACTINT.EXE next, instead
  1551.       else
  1552.      {
  1553.          static char far msg[] =
  1554.          {"Help system was unable to open FRACTINT.HLP!\n"};
  1555.      stopmsg(1, msg);
  1556.      }
  1557. */
  1558.       }
  1559.  
  1560. #endif
  1561.  
  1562.    if ( help_file == -1 )
  1563.       {
  1564.       static char far err_no_open[]    = "Help system was unable to open FRACTINT.EXE!\n";
  1565.       static char far err_no_exe[]     = "Help system couldn't find FRACTINT.EXE!\n";
  1566.       static char far err_not_in_exe[] = "Help not found in FRACTINT.EXE!\n";
  1567.       static char far err_wrong_ver[]  = "Wrong help version in FRACTINT.EXE!\n";
  1568.  
  1569.       if ( find_file("FRACTINT.EXE", path) )
  1570.      {
  1571.      if ( (help_file = open(path, O_RDONLY|O_BINARY)) != -1 )
  1572.         {
  1573.         long help_offset;
  1574.         for (help_offset = -10L; help_offset >= -128L; help_offset--)
  1575.            {
  1576.            lseek(help_file, help_offset, SEEK_END);
  1577.            read(help_file, (char *)&hs, 10);
  1578.            if (hs.sig == HELP_SIG)  break;
  1579.            }
  1580.  
  1581.         if ( hs.sig != HELP_SIG )
  1582.            {
  1583.            close(help_file);
  1584.            help_file = -1;
  1585. /* not an error, but don't complete the open, either
  1586.            stopmsg(1, err_not_in_exe);
  1587. */
  1588.                return(-1);  /* failure */
  1589.            }
  1590.  
  1591.         else if ( hs.version != HELP_VERSION )
  1592.            {
  1593.            close(help_file);
  1594.            help_file = -1;
  1595. /* not an error, but don't complete the open, either
  1596.            stopmsg(1, err_wrong_ver);
  1597. */
  1598.                return(-1);  /* failure */
  1599.            }
  1600.  
  1601.         else
  1602.            base_off = hs.base;
  1603.  
  1604.         }
  1605.      else
  1606. /* not an error, but don't complete the open, either
  1607.         stopmsg(1, err_no_open);
  1608. */
  1609.             return(-1);  /* failure */
  1610.      }
  1611.       else {
  1612. /* not an error, but don't complete the open, either
  1613.      stopmsg(1, err_no_exe);
  1614. */
  1615.          return(-1);  /* failure */
  1616.          }
  1617.       }
  1618.  
  1619.    help_seek(0L);
  1620.  
  1621.    read(help_file, (char *)&max_pages, 2);
  1622.    read(help_file, (char *)&max_links, 2);
  1623.    read(help_file, (char *)&num_topic, 2);
  1624.    read(help_file, (char *)&num_label, 2);
  1625.    help_seek(12L);  /* skip num_contents and num_doc_pages */
  1626.  
  1627.    assert(max_pages > 0);
  1628.    assert(max_links >= 0);
  1629.    assert(num_topic > 0);
  1630.    assert(num_label > 0);
  1631.  
  1632.    /* allocate one big chunk for all three arrays */
  1633.  
  1634.    topic_offset = (long far *)farmemalloc(4L*num_topic + 4L*num_label + sizeof(HIST)*MAX_HIST);
  1635.  
  1636.    if (topic_offset == NULL)
  1637.       {
  1638.       static char far err_no_mem[] = "Not enough memory for help system!\n";
  1639.       close(help_file);
  1640.       help_file = -1;
  1641.       stopmsg(1, err_no_mem);
  1642.  
  1643.       EXIT_OVLY;      /* JIC stopmsg continues for some reason? */
  1644.       return (-2);
  1645.       }
  1646.  
  1647.    /* split off the other arrays */
  1648.  
  1649.    label = (LABEL far *)(&topic_offset[num_topic]);
  1650.    hist  = (HIST far *)(&label[num_label]);
  1651.  
  1652.    /* read in the tables... */
  1653.  
  1654.    farread(help_file, topic_offset, num_topic*4);
  1655.    farread(help_file, label, num_label*4);
  1656.  
  1657.    /* finished! */
  1658.  
  1659.    EXIT_OVLY;
  1660.    return (0);    /* success */
  1661.    }
  1662.  
  1663.  
  1664. void end_help(void)
  1665.    {
  1666.    ENTER_OVLY(OVLY_HELP);
  1667.    if (help_file != -1)
  1668.       {
  1669.       close(help_file);
  1670.       farmemfree((unsigned char far *)topic_offset);
  1671.       help_file = -1;
  1672.       }
  1673.    EXIT_OVLY;
  1674.    }
  1675.  
  1676.  
  1677.